const { DataTypes, Deferrable } = require('sequelize')

const sequelize = require('../db/sequelize')
const Tag = require('./tag.model')

const User = require('./user.model')

const Question = sequelize.define('question', {
  questioner: {
    type: DataTypes.INTEGER,
    references: {
      model: User,
      key: 'id',
      deferrable: Deferrable.INITIALLY_IMMEDIATE
    },
    comment: '提问者的 userId'
  },
  content: {
    type: DataTypes.TEXT,
    allowNull: false,
    comment: '问题的内容'
  },
  weight: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
    comment: '权重查看 +1'
  },
  status: {
    type: DataTypes.BOOLEAN,
    allowNull: false,
    defaultValue: 0,
    comment: '问题的状态(0:显示, 1:隐藏)'
  },
  tags: {
    type: DataTypes.STRING,
    allowNull: true,
    references: {
      model: Tag,
      key: 'name'
    },
    comment: '问题的标签'
  }
}, {
  tableName: 'questions',
  charset: 'utf8'
})

module.exports = Question
